package com.allwees.bs.c.module.order.mapper;

import com.allwees.bs.c.module.order.dto.OrderItemDTO;
import com.allwees.bs.c.module.order.entity.LogisticsWaybillEntity;
import com.allwees.bs.c.module.order.entity.OrderItemEntity;
import com.allwees.bs.c.module.order.entity.OrderRefundEntity;
import com.allwees.bs.c.module.order.entity.OrderSkuSnapEntity;
import com.allwees.bs.c.module.order.repository.LogisticsWaybillEntityRepository;
import com.allwees.bs.c.module.order.repository.OrderItemRepository;
import com.allwees.bs.c.module.order.repository.OrderSkuSnapRepository;
import com.allwees.bs.c.module.order.service.IOrderRefundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class OrderItemMapper {
    @Autowired
    private IOrderRefundService orderRefundService;
    @Autowired
    private OrderItemRepository orderItemRepository;
    @Autowired
    private LogisticsWaybillEntityRepository orderLogisticsRepository;
    @Autowired
    private OrderSkuSnapRepository orderSkuSnapRepository;

    public OrderItemDTO toDto(String orderItemUuid) {
        OrderItemEntity orderItem = orderItemRepository.getByUuid(orderItemUuid);
        return toDto(orderItem);
    }

    public OrderItemDTO toDto(OrderItemEntity orderItem) {

        OrderSkuSnapEntity orderSkuSnap = orderSkuSnapRepository.getByOrderItemUuid(orderItem.getUuid());
        LogisticsWaybillEntity orderItemLogistics = orderLogisticsRepository.findByOrderItemUuid(orderItem.getUuid());

        OrderRefundEntity orderRefund = orderRefundService.getRefundByOrderItemUuid(orderItem.getUuid());

        return new OrderItemDTO(orderItem, orderSkuSnap, orderItemLogistics).setRefundInfo(orderRefund);
    }
}
